import {
  getStaffInfo,
  changeRankAndRole,
  delStaff
} from '../../../utils/networkRequest.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    staffInfo: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;
    let userInfo = JSON.parse(options.userInfo);
    getStaffInfo(userInfo.id, (r) => {
      console.log(r);
      if (r.code == 0) {
        self.setData({
          staffInfo: r.data
        })

      }
    })
  },
  //职位修改
  rankChange: function(e) {
    let {
      staffInfo
    } = this.data;
    let self = this;
    let id = e.currentTarget.dataset.id;
    let roleid = e.currentTarget.dataset.roleid;
    let roles = [];
    
    if (id == undefined){
      staffInfo.ranks.map((o, i) => {
        if (o.is_select) {
          id = o.id;
        }
      })
    }else{
      staffInfo.ranks.map((o, i) => {
        if (o.id == id) {
          o.is_select = true;
        } else {
          o.is_select = false
        }
      })
    }
   

    if (roleid!=undefined){
      staffInfo.roles.map((o, i) => {
        if (o.id == roleid) {
          o.is_select = !o.is_select;
        }
      })
     
    }

    staffInfo.roles.map((o, i) => {
      if (o.is_select) {
        roles.push(o.id);
      }
    })
    

    changeRankAndRole(staffInfo.id, id, roles, (r) => {
      if (r.code == 0) {
        self.setData({
          staffInfo: staffInfo
        })
      }else{
        wx.showToast({
          icon:'none',
          title:r.message
        })
      }
    })
  },
  //删除店员
  delStaff:function(){
    let { staffInfo } = this.data
    wx.showModal({
      title: '提示',
      content: '确定要停用该帐号吗？',
      success: function (res) {
        if (res.confirm) {
         
          delStaff(staffInfo.id, (r) => {
            if (r.code == 0) {
              wx.showToast({
                icon:"none",
                title: '停用成功',
              })
             wx.navigateBack()
            }else{
              wx.showToast({
                icon:"none",
                title:r.message
              })
            }
          })
        } else if (res.cancel) {
          //点击取消
        }
      }
    })
     
  
  }
})